import React from "react";
import { notification as antdNOtification } from "../../antd/index";
import { CloseIcon, ErrorIcon, InfoIcon, SuccessIcon, WarnIcon } from "../Svg";
import "./style/index";
// type
import { NotificationApi, NotificationInstance, IconType, ArgsProps } from "../../antd/notification";
export type { IconType, NotificationPlacement } from "../../antd/notification";
export type Notification = NotificationApi & {};
export type NotificationApiKey = IconType | "warn" | "open";
export type NotificationTypeApi = NotificationInstance & {
  warn(args: ArgsProps): void;
};

const icons: {
  [key in NotificationApiKey]: (() => JSX.Element) | null;
} = {
  success: SuccessIcon,
  info: InfoIcon,
  error: ErrorIcon,
  warning: WarnIcon,
  warn: WarnIcon,
  open: null,
};
const Keys: NotificationApiKey[] = ["success", "info", "error", "warning", "warn", "open"];

// 二次处理api
const api: NotificationTypeApi = Keys.reduce((api, key) => {
  api[key] = (args: ArgsProps) => {
    const icon = icons[key];
    const params = { icon: icon && icon(), closeIcon: <CloseIcon />, ...args };
    antdNOtification[key].call(null, params);
  };
  return api;
}, {} as NotificationTypeApi);

const { close, config, destroy, useNotification } = antdNOtification;
const notification: NotificationApi = {
  ...api,
  close,
  config,
  destroy,
  useNotification,
};

export default notification;
